/*
 *  Copyright (c) 2014 James Buncle
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 * 
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 * 
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 * 
 */
package com.jbuncle.seleniumiderunner.commands;

import com.jbuncle.seleniumiderunner.TestDriver;
import javax.script.ScriptException;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static com.jbuncle.seleniumiderunner.Utils.wildcardToRegex;
import org.openqa.selenium.By;

/**
 *
 * @author James Buncle
 */
public class Assert extends Basic {

    public Assert(final TestDriver driver) {
        super(driver);
    }

    public void assertTitle(final String target) {
        assertEquals(target, this.getTitle());
    }

    public void assertEval(final String target, final String value) {
        try {
            assertEquals(value, this.processJavascript(target));
        } catch (ScriptException ex) {
            throw new RuntimeException(ex);
        }
    }

    public void assertValue(final String target, final String value) {
        assertEquals(value, this.findElement(target).getAttribute("value"));
    }

    private void assertEquals(final String expected, final String actual) {
        assertTrue(actual.matches(wildcardToRegex(expected)));
    }

    public void assertNotValue(final String target, final String value) {
        assertNotEquals(value, this.findElement(target).getAttribute("value"));
    }

    public void assertElementNotPresent(final String target) {
        assertFalse(this.isElementPresent(target));
    }

    public void assertElementPresent(final String target) {
        assertTrue(this.isElementPresent(target));
    }

    public void assertVisible(final String target) {
        assertTrue(this.findElement(target).isDisplayed());
    }

    public void assertNotVisible(final String target) {
        assertFalse(this.findElement(target).isDisplayed());
    }

    public void assertTextPresent(final String target, final String value) {
        assertTrue(bodyTextContains(value));
    }

    public void assertTextNotPresent(final String target, final String value) {
        assertFalse(bodyTextContains(value));
    }

    private boolean bodyTextContains(final String value) {
        final String bodyText = this.findElement(By.cssSelector("BODY")).getText();
        return bodyText.matches("[\\S\\s]*(" + value + ")[\\S\\s]*");
    }

    public void assertText(final String target, final String value) {
        assertEquals(value, this.findElement(target).getText());
    }

    public void assertNotText(final String target, final String value) {
        assertNotEquals(value, this.findElement(target).getText());
    }

    public void assertAttribute(final String target, final String value) {
        final int attrSeperator = target.lastIndexOf("@");
        final String elementSelector = target.substring(0, attrSeperator);
        final String attribute = target.substring(attrSeperator + 1);
        assertEquals(value, this.findElement(elementSelector).getAttribute(attribute));
    }
}
